import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.lijie.ljblog.LjblogApplication;
import com.lijie.ljblog.chess.ChessPoint;
import com.lijie.ljblog.enums.ChessColorEnum;
import com.lijie.ljblog.mapper.BlogMapper;
import com.lijie.ljblog.mapper.PersonMapper;
import com.lijie.ljblog.model.Blog;
import com.lijie.ljblog.model.Climate;
import com.lijie.ljblog.util.RedisUtils;
import com.lijie.ljblog.util.WebUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;


import javax.annotation.Resource;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.GZIPInputStream;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = LjblogApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class test {
    @Resource
    private BlogMapper mapper;
    @Resource
    private WebUtils webUtils;
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    RedisUtils redisUtils;
    @Test
//    @Transactional
    public void test(){
//        webUtils.getLocationByIp("https://devapi.qweather.com/v7/weather/3d?location=120.59241223,31.30356407&key=74ba08f58127476da7375d236a2e4bfd");
        //webUtils.getLocationByIp("https://api.map.baidu.com/location/ip?&coor=bd09ll&ak=Nbr60aM3zSyhXbSuB6YcGPWENONMxLGT&ip=122.97.199.48");
        //https://apis.map.qq.com/ws/location/v1/ip?ip=112.87.128.60&key=KZWBZ-2YWCS-FM4OX-6I4VQ-4XRQS-YQFKE
        ResponseEntity<String> forEntity = restTemplate.getForEntity("https://apis.map.qq.com/ws/location/v1/ip?ip=112.87.128.60&key=KZWBZ-2YWCS-FM4OX-6I4VQ-4XRQS-YQFKE",String.class);
        String body = forEntity.getBody();
        HashMap hashMap = JSONObject.parseObject(body, HashMap.class);
        Map status1 = MapUtils.getMap(hashMap, "status");
//        status1.get()
        Object status = hashMap.get("status");
        System.out.println(body);
    }

    @Test
    public void weatherTest(){
        HttpHeaders httpHeaders = new HttpHeaders();
        // Accept 表示客户端支持什么格式的响应体
        httpHeaders.set("contentType", "application/json;charset=UTF-8");
        // Accept-Encoding 头，表示客户端接收gzip格式的压缩
        httpHeaders.set(HttpHeaders.ACCEPT_ENCODING, "gzip");
        ResponseEntity<byte[]> forEntity = restTemplate.exchange
                ("https://devapi.qweather.com/v7/weather/3d?location=120.59241223,31.30356407&key=74ba08f58127476da7375d236a2e4bfd"
                , HttpMethod.GET, new HttpEntity<>(httpHeaders), byte[].class);
        if(forEntity.getStatusCode()== HttpStatus.OK){
            try {
                InputStream inWithCode = new ByteArrayInputStream(forEntity.getBody());
                GZIPInputStream gunzip = new GZIPInputStream(inWithCode);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                byte[] buffer = new byte[256];
                int n;
                while ((n = gunzip.read(buffer))>= 0) {
                    out.write(buffer, 0, n);
                }
                //gzip解码结束
                String dataStr = out.toString();
                Climate climate = JSONObject.parseObject(dataStr, Climate.class);
                System.out.println(climate);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    @Transactional
    public void update(){
        Blog blog=mapper.selectById(1);
        blog.setTitle("事务测试2");
        mapper.updateById(blog);
//        int i=1/0;

    }

    @Test
    public void redisTest(){
        redisUtils.delete("1");
        String s = redisUtils.get("1");
        System.out.println(s);
    }

    @Test
    public void hh(){
        List<String> list =  Arrays.asList("品种名称","一般","一般","严重","一般","严重","严重","严重","死亡","死亡","合计","合计","死亡");
        List<String> list1 =   Arrays.asList("品种名称","例数","百分比","构成比","例数","百分比","构成比","例数","百分比","构成比","例数","构成比");
        Map<String, ArrayList<Integer>> map = new LinkedHashMap<>();
        Map<String, ArrayList<String>> result = new LinkedHashMap<>();
        for(int i=0;i<list.size();i++){
            String s = list.get(i);
            ArrayList<Integer> integers = map.get(s);
            if(CollectionUtils.isEmpty(integers)){
                integers=new ArrayList<>();
                integers.add(i);
                map.put(s,integers);
            }else {
                integers.add(i);
            }
        }
        map.forEach((key, value)->{
            int start =value.get(0);
            int end =value.get(0);
            ArrayList<String> resultList = new ArrayList<>();
            for(int i=1;i<value.size();i++){
                if(value.get(i)-end==1){
                    end=value.get(i);
                }else{
                    resultList.add(start+"-"+end);
                    start=value.get(i);
                    end=value.get(i);
                }
            }
            resultList.add(start+"-"+end);
            result.put(key,resultList);
        });
        result.forEach((key, value)->{
            System.out.println(key+":" +value);
        });
    }

    @Test
    public void hh1(){
        ChessPoint init = ChessPoint.init(1, 1);
//        init.setColorEnum(ChessColorEnum.CHESS_POINT_BLACK);
        System.out.println(JSONObject.toJSON(init));
    }

    @Test
    public void hh11(){
        ChessPoint chessPoint = JSONObject.parseObject("{\"a\":1,\"c\":1,\"colorEnum\":\"CHESS_POI\"}", ChessPoint.class);
        System.out.println(chessPoint);
    }





}
